/*******************************************************************************
 * Copyright (c) 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//最后点击的区域控件(select)
var arrayCategoryLevelFree=['一级分类','二级分类','三级分类','四级分类','五级分类'];
//仅显示四级分类
var levelFreeCounts=4;
var lastCategoryFreeSelect=null;

function loadAndInitCategoryFree(){
	//加载所需要的JS urls[0]=__ctxPath+"/resource/common/js/jquery/ui/ui.datepicker.js";
	/*<script src="${ctxPath}/dwr/engine.js" type="text/javascript"></script>
	<script src="${ctxPath}/dwr/util.js" type="text/javascript"></script>
	<script src="${ctxPath}/dwr/interface/categoryFreeManager.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctxPath}/resource/common/js/core/selectUtil.js"></script>
	*/
	var jsUrls=new Array();
	jsUrls[0]=__ctxPath+"/dwr/engine.js";
	jsUrls[1]=__ctxPath+"/dwr/util.js";
	jsUrls[2]=__ctxPath+"/dwr/interface/categoryFreeManager.js";
	jsUrls[3]=__ctxPath+"/resource/common/js/core/selectUtil.js";
	$importJs(jsUrls,function(){
	});
	//初始化第一个分类选择框的值；
	lastCategoryFreeSelect=$("#cat1")[0];
	lastCategoryFreeSelect.options[0]=new Option("--"+arrayCategoryLevelFree[0]+"--","");
	lastCategoryFreeSelect.options[0].selected=true;
	//初始化第二个至最后一个选择框的值；
	for(var lv=2;lv<=levelFreeCounts;lv++){
		var categorySelect=$("#cat"+lv)[0];
		fnAddOption(categorySelect,"","--"+arrayCategoryLevelFree[lv-1]+"--");
	}
}
//清空所有子类的显示
function clearNextAllChildCategoryFree(level){
	for(var lv=level;lv<=levelFreeCounts;lv++){
		var category=$("#cat"+lv)[0];
		fnRemoveAllOptions(category);
		fnAddOption(category,"","--"+arrayCategoryLevelFree[lv-1]+"--");	
	}
}
//添充下一个选择框值
function fillNextChildCategoryFree(level, selecteParentId, selecteChildId){
	var presentSelectValue="";
	if (typeof selecteParentId != 'undefined' && selecteParentId.length!=0){
		presentSelectValue = selecteParentId;
	} else {
		presentSelectValue = $("#cat"+level+" option:selected").val();
	}
	
	var nextLevel=level+1;
	
	lastCategoryFreeSelect=$("#cat"+level)[0];
	//清空所有子类的显示
	clearNextAllChildCategoryFree(nextLevel);
	if(presentSelectValue==""){
		return;
	}
	else{
		//显示下一个子类
		categoryFreeManager.getByParentId(parseInt(presentSelectValue),{
			async:false,callback:function(data){
				var sourceObj=$("#cat"+nextLevel)[0];
				for(var i=0;i<data.length;i++){
					fnAddOption(sourceObj,data[i].categoryId,data[i].categoryName);
					if (typeof selecteChildId != 'undefined' 
							&& selecteChildId.length!=0
							&& data[i].categoryId==selecteChildId){
						sourceObj.options[i+1].selected=true;
					}
				}
			}
		});
	}	
}
//添充第一个选择框值
function fillFirstCategoryFree() {
	DWREngine.setAsync(false);
	categoryFreeManager.getMainCategories("true",fillCategoryFree);
}
function fillCategoryFree(data) {
	var sourceObj=$("#cat1")[0];
	for(var i=0;i<data.length;i++){
		fnAddOption(sourceObj,data[i].categoryId,data[i].categoryName);
	}
}
function selectCategoryFree(level, selecteCategoryId) {
	var sourceObj=$("#cat"+level)[0];
	for (var i = 0; i < sourceObj.options.length; i++) {
		if (sourceObj.options[i].value == selecteCategoryId) {
			sourceObj.options[i].selected=true;
		}
	}
}


function selectCategoryFreePath(id){
	categoryFreeManager.getById(id,{async:false,callback:function(data){
		categoryPath=data.path;
		var catIds=categoryPath.split('.');
		if(catIds!=null&&catIds.length>0){
			for(var i=1;i<catIds.length-1;i++){
				selectOption($("#cat"+(i))[0],catIds[i]);
				categoryFreeManager.getByParentId(parseInt(catIds[i]),{async:false,callback:function(data){
					var sourceObj=$("#cat"+(i+1))[0];
					clearNextAllChildCategoryFree(i+1);
					for(var cn=0;cn<data.length;cn++){
						fnAddOption(sourceObj,data[cn].categoryId,data[cn].categoryName);
					}
				}});
			}
			clearNextAllChildCategoryFree(catIds.length-1);
		}
	}})
	
}